home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / MacBob.cp < prev    next >
Encoding:
Text File  |  1995-12-06  |  5.6 KB  |  280 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    MacBob.cp - The Bob programming language as a BBEdit extension.
  4.   *    Copyright © 1995 by Christopher E. Hyde.  All rights reserved.
  5.   *
  6.   ***/
  7.  
  8.     // THINK C
  9. #include <SetupA4.h>
  10. #include <Balloons.h>
  11. #include <GestaltEqu.h>
  12.  
  13. #include "CSavePort.h"
  14. #include "Exceptions.h"
  15.  
  16.     // BBEdit
  17. #include "BBEdit.h"
  18. #include "DialogUtilities.h"
  19.  
  20. #include "Bob.h"
  21.  
  22.     // Global Variables
  23. BBCallbackPtr    BBEdit;
  24. CIStream         gInput;
  25. COStream        gOutput;
  26. TPrefs             gPrefs;
  27.  
  28.     // Local Variables
  29. static Handle pHandles = nil;
  30.  
  31.     // Local Functions
  32. pascal void    main                    (BBCallbackPtr callbacks, WindowPeek w);
  33. static short    MainDialog                (void);
  34. static void    DoAboutBox                (void);
  35. static void    SetDialogFontAndSize    (DialogPtr d, short fontNum, short fontSize);
  36. static void    DoStopAlert                (short msgID);
  37. static void    CleanUp                    (void);
  38.  
  39.  
  40. pascal void 
  41. main (BBCallbackPtr callbacks, WindowPeek w)
  42. {
  43.     RememberA0();
  44.     SetUpA4();
  45.  
  46.     BBEdit = callbacks;
  47.  
  48.     if (BBEdit->version < 2) {
  49.         DoStopAlert(errBBEditVersionNotHighEnough);
  50.         goto finish;
  51.     }
  52.  
  53.     if (!w && w->windowKind != userKind) {
  54.         DoStopAlert(errWrongWindowKind);
  55.         goto finish;
  56.     }
  57.  
  58. #if 0
  59.     long    selStart, selEnd, firstChar;
  60.     BBEdit->GetSelection(&selStart, &selEnd, &firstChar);
  61.     if (selStart == selEnd) {
  62.         DoStopAlert(errNeedSelection);
  63.         goto finish;
  64.     }
  65. #endif
  66.  
  67.     short    prefsSize;
  68.     BBEdit->GetPreference(kPrefsResType, sizeof(gPrefs), &gPrefs, &prefsSize);
  69.  
  70.         // If we couldn't get the prefs, set them to the default
  71.     if (prefsSize <= 0) {
  72.         for (short i = kFirstOption; i < kLastOptionPlus1; ++i)
  73.             _Opt(i) = false;
  74.         Opt(PatchCode)    =
  75.         Opt(BufferStdErr) = true;
  76.     }
  77.  
  78.     if (MainDialog() == kRun) {
  79.         BBEdit->SetPreference(kPrefsResType, sizeof(gPrefs), &gPrefs, &prefsSize);
  80.  
  81.         pHandles = nil;
  82.         gInput.fHandle  =
  83.         gOutput.fHandle = nil;
  84.  
  85.         TRY
  86.             gInput.Open(&w->port);
  87.             gOutput.Open(!Opt(BufferStdErr));
  88.  
  89.             FailNil(BBEdit->NewDocument());
  90.  
  91.                 // Do real Bob stuff
  92.             BobMain();
  93.         CATCH
  94.             if (gFailE == memFullErr)
  95.                 gFailE = errNotEnoughMemory;
  96.             if (gFailE < 0)
  97.                 BBEdit->ReportOSError(gFailE);
  98. #if 0
  99.             else if (gFailE < 0)
  100.                 gFailE = errUnknown;
  101.             DoStopAlert(gFailE);
  102. #else
  103.             if (gFailE > errUnknown)
  104.                 SysBeep(0);
  105.             else
  106.                 DoStopAlert(gFailE);
  107. #endif
  108.         ENDTRY
  109.  
  110.         CleanUp();
  111.     }
  112.  
  113. finish:
  114.     RestoreA4();
  115. }
  116.  
  117.  
  118. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  119.  
  120. #if 0
  121. void*
  122. Malloc (UInt32 size)
  123. {
  124.     return Calloc(size, 1);
  125. }
  126. #endif
  127.  
  128.  
  129. #if 0
  130. static int callocCount = 0;
  131. #endif
  132.  
  133. void*
  134. Calloc (UInt32 count, UInt32 size)
  135. {
  136.     Handle h = BBEdit->Allocate(count * size + sizeof(Handle), true);
  137. #if 0 && qDebug
  138.     if (Opt(Debug))
  139.         PrintErrF("\t• Calloc(%d, %d) => 0x%X\r",
  140.                             count, size, (h == nil) ? nil : &(*h)[sizeof(Handle)]);
  141. #endif
  142. //    ++callocCount;
  143.     FailNil(h);
  144.     HLockHi(h);
  145.     **(Handle**) h = pHandles;
  146.     pHandles = h;
  147.     return &(*h)[sizeof(Handle)];
  148. }
  149.  
  150.  
  151. extern "C" void __malloc_cleanup(void);
  152.  
  153.  
  154. static void
  155. CleanUp (void)
  156. {
  157. #if 0
  158.     for (Handle h = pHandles; callocCount-- && h != nil; ) {
  159.         PrintErrF("DisposeHandle(0x%X)\r", h);
  160. #endif
  161.  
  162.     TRY
  163.         for (Handle nextH, h = pHandles; h != nil; h = nextH) {
  164.             nextH = **(Handle**) h;
  165.             DisposeHandle(h);
  166.         }
  167.  
  168.         gInput.Close();
  169.         gOutput.Close();
  170.     CATCH
  171. #if qDebug
  172.         BBEdit->ReportOSError(gFailE);
  173. #endif
  174.     ENDTRY
  175. }
  176.  
  177.  
  178. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  179.  
  180.  
  181. static short
  182. MainDialog (void)
  183. {
  184.     bool saveBalloonStatus, hasHelpMgr = false;
  185.     long answer;
  186.     if (Gestalt(gestaltHelpMgrAttr, &answer) == noErr) {
  187.         hasHelpMgr = true;
  188.         saveBalloonStatus = HMGetBalloons();
  189.     }
  190.  
  191.     DialogPtr d = BBEdit->CenterDialog(rOptionsDlog);
  192.     CSavePort savePort(d);
  193.  
  194.     InitKeyEquiv(d, rOptionsDlog);
  195.     SetupUserItem(d, kLine, BBEdit->FrameDialogItem);
  196.  
  197.     SetItemEnable(d, kBalloonHelp, hasHelpMgr);
  198.  
  199.     for (short i = kFirstOption; i < kLastOptionPlus1; ++i)
  200.         SetItemValue(d, i, _Opt(i));
  201.  
  202.     short item;
  203.     do {
  204.         ModalDialog(KeyEquivFilter, &item);
  205.  
  206.         if (item == kAbout)
  207.             DoAboutBox();
  208.         else if (item == kBalloonHelp) {
  209.             bool balloons = !HMGetBalloons();
  210.             HMSetBalloons(balloons);
  211.             SetItemIcon(d, kBalloonHelp, balloons ? rHelpIconOn : rHelpIconOff);
  212.         } else if (item >= kFirstOption && item < kLastOptionPlus1)
  213.             _Opt(item) = ToggleItem(d, item);
  214.  
  215.     } while (item != kRun && item != kCancel);
  216.  
  217.     if (hasHelpMgr)
  218.         HMSetBalloons(saveBalloonStatus);
  219.  
  220.     EndKeyEquiv(d);
  221.     DisposeDialog(d);
  222.     return item;
  223. }
  224.  
  225.  
  226. static void 
  227. DoAboutBox (void)
  228. {
  229.     DialogPtr d = BBEdit->CenterDialog(rAboutDlog);
  230.  
  231.     if (d != nil) {
  232.         CSavePort savePort(d);
  233.         SetDialogFontAndSize(d, geneva, 9);
  234.         SetupUserItem(d, kAboutLine, BBEdit->FrameDialogItem);
  235.         short item;
  236.         ModalDialog(BBEdit->StandardFilter, &item);
  237.         DisposeDialog(d);
  238.     }
  239. }
  240.  
  241.  
  242. // SetDialogFontAndSize is a slightly modified version of an original alt.sources.mac code snippet by Leonard Rosenthol.
  243. static void
  244. SetDialogFontAndSize (DialogPtr d, short fontNum, short fontSize)
  245. {
  246.         // set up the port info
  247.     TextFont(fontNum);
  248.     TextSize(fontSize);
  249. //SetDialogFont(fontNum);
  250.  
  251.         // now deal with the static & edit text issues
  252.     FontInfo f;
  253.     GetFontInfo(&f);
  254.     (*DialogPeek(d)->textH)->txFont = fontNum;
  255.     (*DialogPeek(d)->textH)->txSize = fontSize;
  256.     (*DialogPeek(d)->textH)->lineHeight = f.ascent + f.descent + f.leading;
  257.     (*DialogPeek(d)->textH)->fontAscent = f.ascent;
  258. }
  259.  
  260.  
  261. static void 
  262. DoStopAlert (short msgID)
  263. {
  264.     Str255 msg;
  265.     GetIndString(msg, rErrorStrings, msgID);
  266.  
  267.     SysBeep(2);    
  268.     DialogPtr d = BBEdit->CenterDialog(rStopAlertDlog);
  269.     if (d == nil)
  270.         return;
  271.  
  272.     CSavePort savePort(d);
  273.     ParamText(msg, nil, nil, nil);
  274.     short item;
  275.     ModalDialog(BBEdit->StandardFilter, &item);
  276.     DisposeDialog(d);
  277.  
  278. //    aShort = StopAlert(alertID, filterProc);
  279. }
  280.